// JsonRpc框架-rpc功能联调
#include "../../common/dispatcher.hpp"
#include "../../client/requestor.hpp"
#include "../../client/rpc_caller.hpp"
#include <thread>
void callback(const Json::Value& result) {
    DLOG("result = %d", result.asInt());
}
int main()
{
    auto requestor = std::make_shared<rcrpc::client::Requestor>();
    auto caller = std::make_shared<rcrpc::client::RpcCaller>(requestor);

    auto dispatcher = std::make_shared<rcrpc::Dispatcher>();
    auto rsp_cb = std::bind(&rcrpc::client::Requestor::onResponse, requestor.get(),
        std::placeholders::_1, std::placeholders::_2);
    dispatcher->registerHandler<rcrpc::BaseMessage>(rcrpc::MType::RSP_RPC,rsp_cb);

    auto client = rcrpc::ClientFactory::create("127.0.0.1", 9090);
    auto message_cb = std::bind(&rcrpc::Dispatcher::onMessage, dispatcher.get(),
        std::placeholders::_1, std::placeholders::_2);
    client->setMessageCallback(message_cb);
    client->connect();
    
    auto conn = client->connection();
    Json::Value params,result;
    params["num1"] = 12;
    params["num2"] = 21;
    bool ret = caller->call(conn, "Add", params, result);
    if (ret) {
        DLOG("result = %d" , result.asInt());
    }

    rcrpc::client::RpcCaller::JsonAsyncResponse rsp_msg;
    params["num1"] = 121;
    params["num2"] = 21;
    bool ret1 = caller->call(conn, "Add", params, rsp_msg);
    if (ret1) {
        result = rsp_msg.get();
        DLOG("result = %d" , result.asInt());
    }


    params["num1"] = 112;
    params["num2"] = 211;
    bool ret2 = caller->call(conn, "Add", params, callback);
    std::cout << "------\n";
    std::this_thread::sleep_for(std::chrono::seconds(1));
    client->shutdown();
    return 0;

}